package net.jxtz.jxtzos.controller.auth;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.jxtz.jxtzos.entity.auth.Role;
import net.jxtz.jxtzos.entity.web.R;
import net.jxtz.jxtzos.service.auth.RoleService;
import net.jxtz.jxtzos.service.auth.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author a123
 */
@RestController
@CrossOrigin
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public R add(@RequestBody Role role){
        boolean save = roleService.save(role);
        return save ? R.ok():R.error();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public R delete(@PathVariable Long id){
        boolean b = roleService.removeById(id);
        return b ? R.ok():R.error();
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public R list(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, @RequestParam(value = "roleDesc", required = false) String roleDesc){
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        if (!"undefined".equals(roleDesc)){
            roleQueryWrapper.like("role_desc", roleDesc);
        }
        Page<Role> page = roleService.page(new Page<>(pageNum, pageSize), roleQueryWrapper);
        long total = page.getTotal();
        List<Role> records = page.getRecords();
        return R.ok().data("total", total).data("list", records);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R list(){
        List<Role> list = roleService.list();
        return R.ok().data("list", list);
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public R update(@RequestBody Role role){
        boolean b = roleService.updateById(role);
        return b ? R.ok():R.error();
    }

    private final RoleService roleService;

    private final UserRoleService userRoleService;

    @Autowired
    public RoleController(RoleService roleService, UserRoleService userRoleService){
        this.roleService = roleService;
        this.userRoleService = userRoleService;
    }
}
